package com.joysuch.wwyt.attendant.job;

import com.joysuch.wwyt.attendant.entity.AttendantPlan;
import com.joysuch.wwyt.attendant.entity.AttendantRecord;
import com.joysuch.wwyt.attendant.enums.AttendantStateEnum;
import com.joysuch.wwyt.attendant.service.AttendantPlanService;
import com.joysuch.wwyt.attendant.service.AttendantRecordService;
import com.joysuch.wwyt.util.DateUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author xsx
 * @version 1.0
 * @date 2023/2/20 16:38
 */
@Slf4j
@AllArgsConstructor
@Component
public class PlanStateJob {

    @Autowired
    private AttendantPlanService attendantPlanService;

    @Autowired
    private AttendantRecordService attendantRecordService;

    /**
     * 定时任务更新跟班计划状态
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void planStateSyncJob() {
        Date date = DateUtils.getToDay();
        List<AttendantPlan> updateList = new ArrayList<>();
        List<AttendantPlan> updateOverList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<AttendantPlan> list = attendantPlanService.selectByTime(date, 1, 1);
        if (CollectionUtils.isNotEmpty(list)) {
            for (AttendantPlan plan : list) {
                String attendantTimeStr = sdf.format(plan.getAttendantTime());
                String startTimeStr = attendantTimeStr +" "+ plan.getStartTime().toString();
                try {
                    Date startTime = sdf2.parse(startTimeStr);
                    if (startTime.getTime() <= System.currentTimeMillis()) {
                        plan.setState(AttendantStateEnum.HAVE_IN_HAND.getValue());
                        updateList.add(plan);
                        log.info("更新计划状态为进行中id为" + plan.getId());
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }
        }
        List<AttendantPlan> overList = attendantPlanService.selectByTime(date, 3, 0);
        if (CollectionUtils.isNotEmpty(overList)) {
            for (AttendantPlan plan : overList) {
                String attendantTimeStr = sdf.format(plan.getAttendantTime());
                String endTimeStr = attendantTimeStr +" " + plan.getEndTime().toString();
                try {
                    Date endTime = sdf2.parse(endTimeStr);
                    if (plan.getEndTime().getTime()<plan.getStartTime().getTime()){
                        endTime=  getLastDay(endTime);
                    }
                    if (endTime.getTime()<= System.currentTimeMillis()) {
                        plan.setState(AttendantStateEnum.OVERDUE.getValue());
                        updateList.add(plan);
                        updateOverList.add(plan);
                        log.info("更新计划状态为逾期完成id为" + plan.getId());
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }
        }
        if (CollectionUtils.isNotEmpty(updateList)) {
            attendantPlanService.saveOrUpdateBatch(updateList);
        }
        //生成未完成的逾期记录
        if (CollectionUtils.isNotEmpty(updateOverList)) {
            List<AttendantRecord> records=new ArrayList<>();
            for (AttendantPlan plan : updateOverList) {
                AttendantRecord attendantRecord = attendantRecordService.selectByPlanId(plan.getId());
                if (attendantRecord!=null){
                    plan.setState(AttendantStateEnum.COMPLETED.getValue());
                    attendantPlanService.updateById(plan);
                    continue;
                }
                AttendantRecord record = new AttendantRecord();
                record.setAttendantPlanId(plan.getId());
                record.setOrgCode(plan.getOrgCode());
                record.setTenentId(plan.getTenentId());
                records.add(record);
            }
            attendantRecordService.saveBatch(records);
        }
    }

    public Date getLastDay(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        // 把日期往后增加一天,整数  往后推,负数往前移动
        calendar.add(Calendar.DATE, 1);
        // 这个时间就是日期往后推一天的结果
        return calendar.getTime();

    }
}
